/**
 * Copyright (c) 2011 eelpo.com
 * All rights reserved.
 */
package com.eelpo.common.query.expression;

import java.util.Collection;

import com.eelpo.common.util.BlankUtils;

/**
 * <pre>
 * 
 * Member [of]表达式
 * 
 * </pre>
 * 
 * @author <a href="mailto:Brian@eelpo.com">汪洪州</a>
 * @since 1.0
 */
public class MemberOfExpression implements ParameterCriterion {

	private boolean useAlias;
	private String operator;
	private String listProperty;
	private Object value;

	private MemberOfExpression(boolean useAlias, String operator, String listProperty, Object value) {
		this.useAlias = useAlias;
		this.operator = operator;
		this.value = value;
		this.listProperty = listProperty;
	}

	/**
	 * (non-Javadoc)
	 * 
	 * @see com.eelpo.common.query.expression.ParameterCriterion#getJpql(java.lang.
	 *      StringBuilder)
	 */
	@Override
	public StringBuilder getJpql(StringBuilder jpql, String alias) {
		jpql.append(AND);
		jpql.append(PLACEHOLDER);
		jpql.append(operator);
		if (useAlias) {
			jpql.append(alias);
		}
		jpql.append(listProperty);
		return jpql;
	}

	/**
	 * (non-Javadoc)
	 * 
	 * @see com.eelpo.common.query.expression.ParameterCriterion#getParameters(java.util.
	 *      Collection)
	 */
	@Override
	public Collection<Object> getParameters(Collection<Object> parameters) {
		parameters.add(value);
		return parameters;
	}

	/**
	 * 构建表达式
	 * 
	 * @param operator
	 *            运算符
	 * @param propertyName
	 *            属性名
	 * @param parameter
	 *            参数
	 * @return 返回简单
	 */
	public static MemberOfExpression getExpression(boolean useAlias, String operator, String propertyName, Object parameter) {
		if (BlankUtils.isBlank(parameter)) {
			return null;
		}
		return new MemberOfExpression(useAlias, operator, propertyName, parameter);
	}

	/**
	 * 构建表达式
	 * 
	 * @param operator
	 *            运算符
	 * @param propertyName
	 *            属性名
	 * @param parameter
	 *            参数
	 * @return 返回简单
	 */
	public static MemberOfExpression getMemberOfExpression(String propertyName, Object parameter) {
		return getExpression(true, MEMBEROF, propertyName, parameter);
	}

	/**
	 * 构建表达式
	 * 
	 * @param operator
	 *            运算符
	 * @param propertyName
	 *            属性名
	 * @param parameter
	 *            参数
	 * @return 返回简单
	 */
	public static MemberOfExpression getNotMemberOfExpression(String propertyName, Object parameter) {
		return getExpression(true, NOTMEMBEROF, propertyName, parameter);
	}

	/**
	 * 构建表达式
	 * 
	 * @param operator
	 *            运算符
	 * @param propertyName
	 *            属性名
	 * @param parameter
	 *            参数
	 * @return 返回简单
	 */
	public static MemberOfExpression getMemberOfInornateExpression(String propertyName, Object parameter) {
		return getExpression(false, MEMBEROF, propertyName, parameter);
	}

	/**
	 * 构建表达式
	 * 
	 * @param operator
	 *            运算符
	 * @param propertyName
	 *            属性名
	 * @param parameter
	 *            参数
	 * @return 返回简单
	 */
	public static MemberOfExpression getNotMemberOfInornateExpression(String propertyName, Object parameter) {
		return getExpression(false, NOTMEMBEROF, propertyName, parameter);
	}
}
